#!/bin/bash

# UOSplayer安装包更新脚本
# 此脚本用于创建更新后的.deb和.zip安装文件，使用最新构建的版本

set -e

echo "正在创建更新后的UOSplayer安装包..."

# 创建临时目录用于构建安装包
BUILD_DIR="temp_build"
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR"
mkdir -p "$BUILD_DIR/DEBIAN"
mkdir -p "$BUILD_DIR/usr/bin"
mkdir -p "$BUILD_DIR/usr/share/applications"
mkdir -p "$BUILD_DIR/usr/share/icons/hicolor/scalable/apps"
mkdir -p "$BUILD_DIR/usr/share/uosplayer/file_for_read"
mkdir -p "$BUILD_DIR/usr/share/uosplayer/pitrue"
mkdir -p "$BUILD_DIR/usr/share/uosplayer/qml"

# 复制最新的可执行文件
cp release/uosplayer "$BUILD_DIR/usr/bin/uosplayer"
chmod +x "$BUILD_DIR/usr/bin/uosplayer"

# 复制资源文件
cp -r UOSplayer/file_for_read/* "$BUILD_DIR/usr/share/uosplayer/file_for_read/"
cp -r UOSplayer/pitrue/* "$BUILD_DIR/usr/share/uosplayer/pitrue/"
cp -r UOSplayer/qml/* "$BUILD_DIR/usr/share/uosplayer/qml/"

# 复制图标
cp UOSplayer/player.svg "$BUILD_DIR/usr/share/icons/hicolor/scalable/apps/uosplayer.svg"

# 创建桌面入口文件
cat > "$BUILD_DIR/usr/share/applications/uosplayer.desktop" << EOF
[Desktop Entry]
Name=UOSplayer
Name[zh_CN]=UOS播放器
Comment=Media player for UOS
Comment[zh_CN]=UOS媒体播放器
GenericName=Media Player
GenericName[zh_CN]=媒体播放器
Exec=uosplayer %f
Icon=uosplayer
Terminal=false
Type=Application
Categories=AudioVideo;Player;Qt;
MimeType=video/x-theora+ogg;video/x-ogm+ogg;video/x-msvideo;video/x-ms-wmv;video/x-ms-asf;video/x-matroska;video/webm;video/quicktime;video/ogg;video/mpeg;video/mp4;video/mp2t;video/dv;video/3gpp2;video/3gpp;audio/x-wavpack;audio/x-vorbis+ogg;audio/x-speex+ogg;audio/x-scpls;audio/x-opus+ogg;audio/x-ms-wma;audio/x-ms-asx;audio/x-mpegurl;audio/x-matroska;audio/x-flac+ogg;audio/x-flac;audio/x-aiff;audio/webm;audio/wav;audio/riff;audio/ogg;audio/mpeg;audio/mp4;audio/mp3;audio/mp2;audio/flac;audio/basic;audio/aac;audio/ac3;audio/3gpp2;audio/3gpp;application/ogg;
EOF

# 创建Debian包的控制文件
cat > "$BUILD_DIR/DEBIAN/control" << EOF
Package: uosplayer
Version: 1.0.0
Section: video
Priority: optional
Architecture: amd64
Depends: libqt5core5a, libqt5gui5, libqt5widgets5, libqt5multimedia5, libqt5multimediawidgets5, libqt5qml5, libqt5quick5
Maintainer: UOS开发者 <developer@uos.com>
Description: UOS媒体播放器
 一个为UOS/Deepin Linux设计的现代媒体播放器
 具有清晰直观的用户界面。
EOF

# 构建.deb安装包
dpkg-deb --build "$BUILD_DIR" "Install/UOSplayer-1.0.0.deb"

# 创建分发zip包
DIST_DIR="temp_dist"
rm -rf "$DIST_DIR"
mkdir -p "$DIST_DIR"

# 从临时提取目录复制文件（保留原始脚本）
cp temp_extract/INSTALL.md "$DIST_DIR/"
cp temp_extract/install.sh "$DIST_DIR/"
cp temp_extract/README.md "$DIST_DIR/"
cp temp_extract/uninstall.sh "$DIST_DIR/"

# 复制新的.deb包
cp Install/UOSplayer-1.0.0.deb "$DIST_DIR/"

# 创建新的分发zip包
rm -f Install/UOSplayer-1.0.0-dist.zip
cd "$DIST_DIR" && zip -r ../Install/UOSplayer-1.0.0-dist.zip * && cd ..

# 清理临时文件
rm -rf "$BUILD_DIR" "$DIST_DIR" "temp_extract"

echo "更新后的安装包已成功创建："
echo "- Install/UOSplayer-1.0.0.deb"
echo "- Install/UOSplayer-1.0.0-dist.zip" 